package io.rehuo.modules.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.rehuo.common.exception.RRException;
import io.rehuo.common.utils.StringUtils;
import io.rehuo.modules.app.dao.ChannelDao;
import io.rehuo.modules.app.entity.ChannelEntity;
import io.rehuo.modules.app.entity.ConfigEntity;
import io.rehuo.modules.app.service.ChannelService;
import io.rehuo.modules.app.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("channelService")
public class ChannelServiceImpl extends ServiceImpl<ChannelDao, ChannelEntity> implements ChannelService {

    @Autowired
    private ConfigService configService;

    @Override
    public void checkChannel(String channel) {
        if (StringUtils.isNotBlank(channel)){
            ChannelEntity channelEntity = baseMapper.selectOne(new QueryWrapper<ChannelEntity>().eq("code", channel));
            if (ObjectUtil.isNull(channelEntity)){
                throw new RRException("system error");
            }
        }else{
            ConfigEntity configEntity = configService.selectOne();
            if (configEntity.getOpenChannel().equals("1")){
                throw new RRException("system error");
            }
        }

    }
}
